<template>
    <div>
        <el-table border
                  :data="tableData"
                  stripe
                  fit
                  style="width: 100%;height: 100%"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    label="序号"
                    type="index"
                    width="50"
                    align="center">
                <template slot-scope="{row, $index}">
                    <span>{{(curPage - 1) * 10 + $index + 1}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="taskName" label="任务名称" :min-width="150">
                <template slot-scope="{row, $index}">
                    <span>{{row.taskName}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="remark" label="任务描述" :min-width="180">
                <template slot-scope="{row, $index}">
                    <span>{{row.remark}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="userId" label="上传人" :min-width="100">
                <template slot-scope="{row, $index}" v-if="row.users">
                    <span>{{row.users.userName}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="updateTime" label="数据上传时间" :min-width="160">
                <template slot-scope="{row, $index}">
                    <span>{{row.updateTime}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="updateUrl" label="最新上传数据包" :min-width="150">
                <template slot-scope="{row, $index}">
                    <el-button v-if="row.updateUrl" class="el-button el-button--mini el-button--primary"
                               @click="download(row.updateUrl)">
                        下载
                    </el-button>
                    <span v-else>无数据包</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="state" label="质检状态" :min-width="150">
                <template slot-scope="{row, $index}">
                    <span>{{state[row.state - 1]}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="data" label="操作" :min-width="180">
                <template slot-scope="{row, $index}">
                    <el-button class="el-button el-button--mini el-button--success" @click="startQc(row.id)"
                               v-if="row.state === 4">
                        开始质检
                    </el-button>
                    <el-button class="el-button el-button--mini el-button--primary" @click="opDialog(row.id)"
                               v-else-if="row.state === 6">
                        数据入库
                    </el-button>
                    <span v-else>等待数据重新上传</span>
                </template>
            </el-table-column>
        </el-table>
        <msg-dialog @yes="updateQc(7)" @no="onCancel" :dialog-visible="dialogVisible"
                    :msg="msg"></msg-dialog>
    </div>
</template>
<script>
    import MsgDialog from "../menu/MsgDialog";
    import qs from "qs";

    export default {
        name: "QcTable",
        components: {
            MsgDialog
        },
        data() {
            return {
                msg: '',
                editTaskName: false,
                editRemark: false,
                multipleSelection: [],
                state: ["新发布", "已认领", "数据有上传", "等待质检", "质检不合格", "质检合格", "数据入库"],
                dialogVisible: false,
                chooseId: -1,
            }
        },
        props: {
            tableData: {
                type: Array,
                defult: []
            },
            curPage: {
                    type: Number,
                    defult: 1
                }
        },
        methods: {
            updateQc(state) {
                this.userId = this.$store.state.user == undefined ? -1 : JSON.parse(this.$store.state.user).id;
                axios.post("/bjdzms/releaseTask/upd1", qs.stringify({
                    'userId': this.userId,
                    'state': state,
                    'type': this.chooseId
                })).then(res => {
                    if (res.data.code == 0) {
                        this.$emit('updateData');
                        this.$message({
                            message: res.data.msg,
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'error'
                        });
                    }
                    this.dialogVisible = false;
                });
            },
            download(url) {
                window.open(url);
            },
            startQc(id) {
                this.chooseId = id;
                this.$emit('startQc', this.chooseId);
            },
            opDialog(id) {
                this.msg = '确认将此条数据入库？';
                this.dialogVisible = true;
                this.chooseId = id;
            },
            to(path) {
                this.$router.push({name: path});
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.taskTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.taskTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            filterHandler(value, row, column) {
                const property = column['property'];
                return row[property] === value;
            },
            resetDateFilter() {
                this.$refs.taskTable.clearFilter('data');
            },
            clearFilter() {
                this.$refs.taskTable.clearFilter();
            },
            onCancel() {
                this.dialogVisible = false;
                this.chooseId = -1;
            },
        },
    }
</script>

<style scoped>

</style>
